﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PTop.Domain.ViewModel.Wechat;

namespace PTop.Front.Areas.Wechat.Models
{
    public class NewsMessage : WechatMsgBase
    {
        private List<NewsItemTemp> _items;
        private static string _template;
        private static string _itemTemplate;

        public NewsMessage(List<NewsItemTemp> items)
        {
            _items = items;
        }

        public override string Template
        {
            get
            {
                if (string.IsNullOrEmpty(_template))
                {
                    _template =  @"<xml>
                                    <ToUserName><![CDATA[{0}]]></ToUserName>
                                    <FromUserName><![CDATA[{1}]]></FromUserName>
                                    <CreateTime>{2}</CreateTime>
                                    <MsgType><![CDATA[news]]></MsgType>
                                    <ArticleCount>{3}</ArticleCount>
                                    <Articles>
                                    {4}
                                    </Articles>
                                    </xml>";            
                }
                return _template;
            }
        }

        public string ItemTemp
        {

            get
            {
                if (string.IsNullOrEmpty(_itemTemplate))
                {
                    _itemTemplate = @"<item>
                        <Title><![CDATA[{0}]]></Title> 
                        <Description><![CDATA[{1}]]></Description>
                        <PicUrl><![CDATA[{2}]]></PicUrl>
                        <Url><![CDATA[{3}]]></Url>
                        </item>";
                }
                return _itemTemplate;
            }
        }

         public override string HandleRequest()
        {
            throw new NotImplementedException();
        }

        public string GenerateContent()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < this._items.Count(); i++)
            {
                sb.Append(string.Format(this.ItemTemp, _items[i].Title, _items[i].Description, _items[i].PicUrl, _items[i].Url));
            }
            var str = string.Format(Template, this.ToUserName, this.FromUserName, WeChatCommon.GetNowTime(), this._items.Count,sb.ToString());
            return str;
        }

        public override string MsgType
        {
            get { return "news"; }
        }
    }
}